package com.shawn.baseappbygroovydsl.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatSeekBar;

import com.shawn.baseappbygroovydsl.R;
import com.shawn.baseappbygroovydsl.utils.UtilCommon;


public class TextSeekbar extends AppCompatSeekBar {


    private Paint textPaint;

    public TextSeekbar(Context context) {
        super(context);
    }

    public TextSeekbar(Context context, AttributeSet attrs) {
//        super(context,attrs);
        this(context, attrs, R.style.MySeekbarSytle);
    }

    public TextSeekbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textPaint = new Paint();
        textPaint.setTextAlign(Paint.Align.CENTER);
        int textSize = UtilCommon.dp2px(context, 16);
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.parseColor("#FFFFFF"));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setThumb(getNewDrawable(getContext(),R.drawable.verify_slide_thumb,UtilCommon.dp2px(getContext(), 30),UtilCommon.dp2px(getContext(), 30)));
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
        float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
        int baseLineY = (int) (getHeight() / 2 - top / 2 - bottom / 2);//基线中间点的y轴计算公式
        canvas.drawText("向右滑动完成拼图解锁红包", getWidth() / 2, baseLineY, textPaint);
    }

    private BitmapDrawable getNewDrawable(Context context, int restId, int dstWidth, int dstHeight) {
//        InputStream is = getResources().openRawResource(restId);
//        BitmapDrawable bd = new BitmapDrawable(context.getResources(),is);
//        Bitmap bm = bd.getBitmap();
        Bitmap bm = BitmapFactory.decodeResource(context.getResources(), restId);
        Bitmap bmp = Bitmap.createScaledBitmap(bm, dstWidth, dstHeight, true);
        BitmapDrawable d = new BitmapDrawable(context.getResources(), bmp);
        Bitmap bitmap = d.getBitmap();
        if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
            d.setTargetDensity(context.getResources().getDisplayMetrics());
        }
        return d;
    }
}
